package com.seda.fdc.ejb.reports.bollettini.esempio;


import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.krysalis.barcode4j.HumanReadablePlacement;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.impl.code128.Code128Constants;
import org.krysalis.barcode4j.impl.code128.EAN128Bean;
import org.krysalis.barcode4j.impl.datamatrix.DataMatrixBean;
import org.krysalis.barcode4j.impl.datamatrix.SymbolShapeHint;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.MimeTypes;

import com.lowagie.text.Font;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper; 
import com.seda.fdc.ejb.reports.pdfgenerator.lowagie.text.BaseFontFactory;
import com.seda.fdc.ejb.reports.pdfgenerator.lowagie.text.SimpleTextBox;
import com.seda.fdc.ejb.reports.pdfgenerator.lowagie.text.image.SimpleImageBox;
import com.seda.fdc.ejb.reports.pdfgenerator.lowagie.util.PdfUtils;
 


public class Bollettino896  {
	private static int pageHeightMillimiters = 297;
	//private static  DecimalFormat _2Digits = new DecimalFormat("00");
	private static  DecimalFormat _6Digits = new DecimalFormat("000000");
	private static  DecimalFormat _10Digits = new DecimalFormat("0000000000");
	private static  DecimalFormat _12Digits = new DecimalFormat("000000000000");
	private static  DecimalFormat _18Digits = new DecimalFormat("000000000000000000");
	private static  DecimalFormat _num2Format = new DecimalFormat("0000000000");
		
	//private static DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
  
	int numeroRate;  
	String rataDocumento18; 
	 
	public void generatePdf(   
			String filePath ) throws Exception {
		
		
		String var, var1;   
		 

		ByteArrayOutputStream temp;
 
		List<InputStream> islist =  new ArrayList<InputStream>();
		
		File file =  new File( filePath ); 

		RataDocumento rataTotale = new RataDocumento();
		BigDecimal totale = new BigDecimal(0); 
		
//		rataTotale.setImportoRata(totale);
//		rataTotale.setNumeroRAV(documento.getNumeroRAV());
//		rataTotale.setScadenzaRata(documento.getDataScadenzaPrimaRata());
		
		var = "TOTALE";
		temp = printPrimaRata(  var );
		
		islist.add(  new ByteArrayInputStream( temp.toByteArray() ) );	
			
		
		 
		PdfUtils.concatInputStream( islist, file);
		  
	}

	  
	private InputStream getTemplateInputStreamPrimaRata() throws FileNotFoundException  {

		InputStream isInputStream =  new FileInputStream(  "file/896_Soris_prima_rata.pdf");
			//Bollettino896.class.getResourceAsStream("file/896_Soris_prima_rata.pdf");
		
		return isInputStream;
	}
	
	
	  
	  
	private byte[] getInfoImageByteArray() throws IOException  {

		InputStream isInputStream =  new FileInputStream( "file/header.png"); 
		 
		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
 
		int nRead; 
		byte[] data = new byte[16384]; 
		
		while ((nRead = isInputStream.read(data, 0, data.length)) != -1) {

		  buffer.write(data, 0, nRead);

		}
		buffer.flush();

		return buffer.toByteArray(); 
	}
	 
	  
	   
	 
	 
	

	private void printOverSingolaRata(   PdfContentByte over) throws Exception{
	
		//INIZIO  SETTAGGIO I FONT 
		BaseFont arialBF = BaseFontFactory.createFont(
							BaseFontFactory.ARIAL,
							BaseFont.WINANSI, true); 
		 
		BaseFont OCRB10PitchBT_Regular = BaseFontFactory.createFont(
							BaseFontFactory.OCRB10PitchBT_Regular,
							BaseFont.WINANSI, true);
		
		BaseFont _601BF = BaseFontFactory.createFont(
				BaseFontFactory._601,
				BaseFont.WINANSI, true); 
		
	
		BaseFont courierNewBF = BaseFontFactory.createFont(
				BaseFontFactory.COURIER_NEW,
				BaseFont.WINANSI, true); 

		//Font arial8 = new Font( arialBF, 8, Font.NORMAL); 
		Font c601_9 = new Font( _601BF, 9, Font.BOLD);
		Font arial9N = new Font( arialBF, 9, Font.NORMAL);
		Font arialC_10 = new Font( arialBF, 10, Font.ITALIC);
		Font OCRB10PitchBT_Regular6 = new Font( OCRB10PitchBT_Regular, 6, Font.NORMAL);
		Font OCRB10PitchBT_Regular10 = new Font( OCRB10PitchBT_Regular, 10, Font.NORMAL);
		Font OCRB10PitchBT_Regular12 = new Font( OCRB10PitchBT_Regular, 12, Font.NORMAL); 
		Font arialC_7 = new Font( arialBF, 7, Font.ITALIC);
		Font arial7N = new Font( arialBF, 7, Font.NORMAL);
		
		Font arial7 = new Font( arialBF, 7, Font.NORMAL);
		Font arial6p = new Font( arialBF,  5.5f, Font.BOLDITALIC);
		Font arial9 = new Font( arialBF, 9, Font.BOLD);
		Font courierNew7 = new Font( courierNewBF, 7, Font.NORMAL);		 
		//FINE SETTAGGIO I FONT 
		
		
		SimpleTextBox numeroCC = new SimpleTextBox(5,27); 
		numeroCC.setLeading(7.5);
		numeroCC.setRotation(270);
		numeroCC.setBorderWidth(0);
		numeroCC.setFont( OCRB10PitchBT_Regular12 );
//		Integer a = new Integer(documento.getNumeroContoCorrente());
		Integer a = new Integer("12345678");
		String b =a.toString();
		numeroCC.addLine(b);
		numeroCC.setAbsolutePositionTop (87, 16,  over, pageHeightMillimiters ); 
		 
		SimpleTextBox importo = new SimpleTextBox(5,25); 
		importo.setLeading(7.5);
		importo.setRotation(270);
		importo.setBorderWidth(0);
		importo.setFont( OCRB10PitchBT_Regular12 );
//		importo.addLine(formatDecimalNumber(rataDocumento.getImportoRata().divide(new BigDecimal(100)))); 
		importo.addLine(formatDecimalNumber( new BigDecimal(111)  )); 
		importo.setAbsolutePositionTop (87, 57.5f,  over, pageHeightMillimiters ); 
		 
		SimpleTextBox intestatoA_1 = new SimpleTextBox(3.5f,77); 
		intestatoA_1.setLeading(7.5);
		intestatoA_1.setRotation(270);
		intestatoA_1.setBorderWidth(0);
		intestatoA_1.setFont( OCRB10PitchBT_Regular6 );
		String intestatoA_1_string = "";
		String intestatoA_1_All = "";
//		intestatoA_1_All = documento.getDenominazioneContoCorrente().trim();
		intestatoA_1_All = "Intestatario";
		intestatoA_1_string = "Intestatario" ; // documento.getDenominazioneContoCorrente().trim();
		if (intestatoA_1_string.length() > 55) {
			intestatoA_1_string = intestatoA_1_string.substring(0, 55);
			intestatoA_1.addLine(intestatoA_1_string);  //documento.getDenominazioneContoCorrente()
			intestatoA_1.setAbsolutePositionTop (74, 6.8f,  over, pageHeightMillimiters );
			
			SimpleTextBox intestatoB_1 = new SimpleTextBox(3.5f,77); 
			intestatoB_1.setLeading(7.5);
			intestatoB_1.setRotation(270);
			intestatoB_1.setBorderWidth(0);
			intestatoB_1.setFont( OCRB10PitchBT_Regular6 );
			intestatoB_1.addLine(intestatoA_1_All.substring(55) );  //documento.getDenominazioneContoCorrente()
			intestatoB_1.setAbsolutePositionTop (72, 6.8f,  over, pageHeightMillimiters );
		} else {
			intestatoA_1.addLine( "Intestatario" );  //documento.getDenominazioneContoCorrente()
			intestatoA_1.setAbsolutePositionTop (74, 6.8f,  over, pageHeightMillimiters );
		}
		
		SimpleTextBox eseguito_da = new SimpleTextBox(4,80); 
		eseguito_da.setLeading(7.5);
		eseguito_da.setRotation(270);
		eseguito_da.setBorderWidth(0);
		eseguito_da.setFont( courierNew7 );
		String eseguito = ""; 
		
		eseguito = "Lardera Roberto";
 
		eseguito_da.addLine( eseguito );
		eseguito_da.setAbsolutePositionTop (62, 7,  over, pageHeightMillimiters ); 
		 
		SimpleTextBox codiceFiscale = new SimpleTextBox(4,75); 
		codiceFiscale.setLeading(7.5);
		codiceFiscale.setRotation(270);
		codiceFiscale.setBorderWidth(0);
		codiceFiscale.setFont( courierNew7 );
		codiceFiscale.addLine("Cod.Fisc./Part.IVA: " + "LRDRRT77HPIPPO47J"); 
		codiceFiscale.setAbsolutePositionTop (58, 7,  over, pageHeightMillimiters ); 
		 
		String numdoc = "numeroDoc" ;
		SimpleTextBox numeroDoc = new SimpleTextBox(4,75); 
		numeroDoc.setLeading(7.5);
		numeroDoc.setRotation(270);
		numeroDoc.setBorderWidth(0);
		numeroDoc.setFont( courierNew7 );
		numeroDoc.addLine("Numero documento: " + numdoc);
		numeroDoc.setAbsolutePositionTop (55, 7,  over, pageHeightMillimiters ); 
		 
		SimpleTextBox primaRata_1 = new SimpleTextBox(5,75); 
		primaRata_1.setLeading(7.5);
		primaRata_1.setRotation(270);
		primaRata_1.setBorderWidth(0);
		primaRata_1.setFont( arial9 );
		primaRata_1.addLine("TOTALE");  
		primaRata_1.setAbsolutePositionTop (38, 7,  over, pageHeightMillimiters );
		
		SimpleTextBox dataScadenza_1 = new SimpleTextBox(4,75); 
		dataScadenza_1.setLeading(7.5);
		dataScadenza_1.setRotation(270);
		dataScadenza_1.setBorderWidth(0);
		dataScadenza_1.setFont( arial7 );
		dataScadenza_1.addLine("Scadenza " + formatCalendarData( new GregorianCalendar() ,	"dd/MM/yyyy"));  
		dataScadenza_1.setAbsolutePositionTop (33, 7,  over, pageHeightMillimiters );
		
		SimpleTextBox numeroRata_1 = new SimpleTextBox(4,75); 
		numeroRata_1.setLeading(7.5);
		numeroRata_1.setRotation(270);
		numeroRata_1.setBorderWidth(0);
		numeroRata_1.setFont( arial7 );
		numeroRata_1.addLine("Numero " + format18Digits("17689769"));  
		numeroRata_1.setAbsolutePositionTop (30, 7,  over, pageHeightMillimiters ); 
		
		
		//--------------------- box 2 --------------------------------
		
		SimpleTextBox numeroCC_2 = new SimpleTextBox(5,50); 
		numeroCC_2.setLeading(7.5);
		numeroCC_2.setRotation(270);
		numeroCC_2.setBorderWidth(0);
		numeroCC_2.setFont( OCRB10PitchBT_Regular12 );
		Integer a2 = new Integer(1234543656) ;
		String b2 =a2.toString();
		numeroCC_2.addLine(b2);
		numeroCC_2.addLine("123234324234"); 
		numeroCC_2.setAbsolutePositionTop (87, 115.5f,  over, pageHeightMillimiters ); 
		
		SimpleTextBox importo_2 = new SimpleTextBox(5,25); 
		importo_2.setLeading(7.5);
		importo_2.setRotation(270);
		importo_2.setBorderWidth(0);
		importo_2.setFont( OCRB10PitchBT_Regular12 );
		importo_2.addLine(formatDecimalNumber( new BigDecimal( 111 ) ) ); 
		importo_2.setAbsolutePositionTop (87, 198,  over, pageHeightMillimiters ); 
		
		SimpleTextBox numeroRata_All = new SimpleTextBox(5,47); 
		numeroRata_All.setLeading(7.5);
		numeroRata_All.setRotation(270);
		numeroRata_All.setBorderWidth(0);
		numeroRata_All.setFont( OCRB10PitchBT_Regular12 );
		numeroRata_All.addLine("numero rataz");
		numeroRata_All.setAbsolutePositionTop (54, 90.5f,  over, pageHeightMillimiters ); 
		
		SimpleTextBox dataScadenza_2 = new SimpleTextBox(5,16); 
		dataScadenza_2.setLeading(7.5);
		dataScadenza_2.setRotation(270);
		dataScadenza_2.setBorderWidth(0);
		dataScadenza_2.setFont( c601_9 );
		dataScadenza_2.addLine( formatCalendarData(new GregorianCalendar(), "dd/MM/yyyy"));
		dataScadenza_2.setAbsolutePositionTop (61.5f, 160.5f,  over, pageHeightMillimiters ); 
		
		rataDocumento18=format18Digits("123213");
		String numRata = rataDocumento18.substring(0, 3) + " " + rataDocumento18.substring(3, 6) + " " + rataDocumento18.substring(6, 9) + " " + rataDocumento18.substring(9, 12) + " " + rataDocumento18.substring(12, 15) + " " + rataDocumento18.substring(15, 18); 
		SimpleTextBox numeroRata_2 = new SimpleTextBox(5,33); 
		numeroRata_2.setLeading(7.5);
		numeroRata_2.setRotation(270);
		numeroRata_2.setBorderWidth(0);
		numeroRata_2.setFont( c601_9 );
		numeroRata_2.addLine(numRata);
		numeroRata_2.setAbsolutePositionTop (61.5f, 193,  over, pageHeightMillimiters ); 
		 
		
		
		SimpleTextBox numeroDocumento_2 = new SimpleTextBox(4,75); 
		numeroDocumento_2.setLeading(7.5);
		numeroDocumento_2.setRotation(270);
		numeroDocumento_2.setBorderWidth(0);
		numeroDocumento_2.setFont( courierNew7 );
		numeroDocumento_2.addLine("Numero documento: " + numdoc);
		numeroDocumento_2.setAbsolutePositionTop (46, 146,  over, pageHeightMillimiters ); 
		 
		/*SimpleTextBox barcode_2 = new SimpleTextBox(5,27); 
		barcode_2.setLeading(7.5);
		barcode_2.setRotation(90);
		barcode_2.setBorderWidth(0);
		barcode_2.setFont( OCRB10PitchBT_Regular10 );
		barcode_2.addLine(documento.getNumeroContoCorrente());  
		barcode_2.setAbsolutePositionTop (117, 210,  over, pageHeightMillimiters ); */
		
		StringBuffer numeroCodiceBarre = new StringBuffer();
		numeroCodiceBarre.append("18");
		numeroCodiceBarre.append( "0123456789012345678" );	//lunghezza 18
		numeroCodiceBarre.append("12");
		numeroCodiceBarre.append( "012345678912" ); //lunghezza 12
		numeroCodiceBarre.append("10");
		numeroCodiceBarre.append( _10Digits.format( 11 ) ); //lunghezza 10
		numeroCodiceBarre.append("3");
		numeroCodiceBarre.append("896");
		
		

		
		SimpleTextBox numeroCodiceBarreTb = new SimpleTextBox(5,95); 
		numeroCodiceBarreTb.setLeading(7.5);
		numeroCodiceBarreTb.setRotation(270);
		numeroCodiceBarreTb.setBorderWidth(0);
		numeroCodiceBarreTb.setFont( arial9N );
		numeroCodiceBarreTb.addLine( numeroCodiceBarre.toString() );  
		numeroCodiceBarreTb.setAbsolutePositionTop (23, 146,  over, pageHeightMillimiters ); 
		
		   
		
		SimpleTextBox num4 = new SimpleTextBox(5,13); 
		num4.setLeading(7.5);
		num4.setRotation(270);
		num4.setBorderWidth(0);
		num4.setFont( OCRB10PitchBT_Regular12 );
		num4.addLine("896>");  
		num4.setAbsolutePositionTop (5, 231.5f,  over, pageHeightMillimiters ); 
		 
		
		SimpleTextBox primaRata = new SimpleTextBox(5,50); 
//		primaRata.setLeading(7.5);
		primaRata.setRotation(270);
		primaRata.setBorderWidth(0);
		primaRata.setFont( arial9 );
		primaRata.addLine("TOTALE");   
		//primaRata.addLine( numeroIngiunzione );   
		primaRata.setAbsolutePositionTop (68, 146,  over, pageHeightMillimiters ); 
		
		
//------------------- box sinistro esterno ---------------------------------------------
		
		SimpleTextBox attenzione = new SimpleTextBox(94,4); 
		attenzione.setLeading(7.5);
		attenzione.setRotation(0);
		attenzione.setBorderWidth(0);
		attenzione.setFont( arialC_10 );
		attenzione.addLine("Attenzione: per i pagamenti presso Poste Italiane ritagliare ");
		attenzione.setAbsolutePositionTop (5, 250,  over, pageHeightMillimiters ); 
		
		SimpleTextBox attenzione2 = new SimpleTextBox(60,4); 
		attenzione2.setLeading(7.5);
		attenzione2.setRotation(0);
		attenzione2.setBorderWidth(0);
		attenzione2.setFont( arialC_10 );
		attenzione2.addLine("lungo la linea tratteggiata");
		attenzione2.setAbsolutePositionTop (29, 254,  over, pageHeightMillimiters ); 
		
		 
		
		SimpleTextBox numeroDocumento_esterno = new SimpleTextBox(52,5); 
		numeroDocumento_esterno.setLeading(7.5);
		numeroDocumento_esterno.setRotation(0);
		numeroDocumento_esterno.setBorderWidth(0);
		numeroDocumento_esterno.setFont( arial9 );
		numeroDocumento_esterno.addLine(numRata);   
		numeroDocumento_esterno.setAbsolutePositionTop (45, 267,  over, pageHeightMillimiters ); 
		
		SimpleImageBox barcode128ImageBox =  new SimpleImageBox(93, 93); 
		barcode128ImageBox.setBorderWidth(0); 
		barcode128ImageBox.addImage( generateBarCode128(    )  ); 
		barcode128ImageBox.setRotation(90);  
		barcode128ImageBox.setAbsolutePositionTop ( -12,  144,  over, pageHeightMillimiters );	

		SimpleImageBox dataMatrixImageBox =  new SimpleImageBox(45, 45); 
		dataMatrixImageBox .setBorderWidth(0); 
		dataMatrixImageBox .addImage( generateDataMatrix( numeroCodiceBarre.toString() )   ); 
		dataMatrixImageBox .setRotation(90);  
		dataMatrixImageBox .setAbsolutePositionTop ( -9.5f,  34,  over, pageHeightMillimiters );
		 
		SimpleImageBox attenzione_CENTR1 = new SimpleImageBox(100,100); 
		attenzione_CENTR1.setBorderWidth(0);
		attenzione_CENTR1.setDpiImage(90);
		attenzione_CENTR1.setRotation(180);
		attenzione_CENTR1.addImage( getInfoImageByteArray() );
		attenzione_CENTR1.setAbsolutePositionTop (103.5f, 5.7f,  over, pageHeightMillimiters );
		   
		
		SimpleTextBox autorizzazione = new SimpleTextBox(40,3); 
		autorizzazione.setLeading(7.5);
		autorizzazione.setRotation(0);
		autorizzazione.setBorderWidth(0);
		autorizzazione.setFont( arial6p );
		autorizzazione.addLine("art. 324/4");   //autorizzazioneHost
		autorizzazione.setAbsolutePositionTop (23, 241,  over, pageHeightMillimiters ); 
		
		//CG RE130450		
 
		StringBuffer numeroCodiceBarreGS1_128 = new StringBuffer();
		numeroCodiceBarreGS1_128.append("415");
		numeroCodiceBarreGS1_128.append( "8050450810010" );	//stringa fissa identificante Soris
		numeroCodiceBarreGS1_128.append("8020");
		numeroCodiceBarreGS1_128.append( format18Digits(rataDocumento18)  ); 
		numeroCodiceBarreGS1_128.append("3902"); 
			numeroCodiceBarreGS1_128.append("000000"); 
		
		SimpleTextBox numeroCodiceBarreGS1_128_string = new SimpleTextBox(77,3); 
		String impo_string="";
		 
		impo_string="000000"; 
		
		numeroCodiceBarreGS1_128_string.setLeading(7.5);
		numeroCodiceBarreGS1_128_string.setRotation(0);
		numeroCodiceBarreGS1_128_string.setBorderWidth(0);
		numeroCodiceBarreGS1_128_string.setFont( arial7N );
		numeroCodiceBarreGS1_128_string.addLine("(415) 8050450810010 (8020) " + format18Digits(rataDocumento18) + " (3902) " + impo_string);
		numeroCodiceBarreGS1_128_string.setAbsolutePositionTop (120, 288,  over, pageHeightMillimiters ); 
		
		//CG Portata larghezza barcode da 10 a 13 come da email di R. RICCI del 07/08/2013.
		SimpleImageBox barcodeGS1_128 =  new SimpleImageBox(81.5, 13); 
		barcodeGS1_128.setBorderWidth(0);  
		barcodeGS1_128.addImage( generateEAN128(   numeroCodiceBarreGS1_128.toString() )   ); 
		barcodeGS1_128.setRotation(0);  
		barcodeGS1_128.setAbsolutePositionTop ( 115,  274,  over, pageHeightMillimiters );
	}
	
 

 
/**
 * formatta l'importo in euro
 * @param s
 * @return
 */
	private String formatNum2(BigDecimal importo){ 
		String temp = _num2Format.format( importo );
		
		temp = temp.substring(0, 8) + "+" + temp.substring(8);
		
		return temp;
	}
	
	private String format12Digits(String s){
		int i = Integer.parseInt(s);
		
		return _12Digits.format( i );
	}
	
	/*
	private String format10Digits(String s){
		int i = Integer.parseInt(s);
		
		return _10Digits.format( i );
	}*/
	
	
	private String format18Digits(String s){
		long i = Long.parseLong(s);
		
		return _18Digits.format( i );
	}

	 /*
	private ByteArrayOutputStream print(Documento documento , RataDocumento rcv) throws Exception{
		ByteArrayOutputStream result =  new ByteArrayOutputStream();
		PdfReader reader;  
		PdfContentByte over;
		InputStream templateInputStream;
		
		templateInputStream = null; // getTemplateInputStream() ;
		
		reader = new PdfReader( templateInputStream );
		
		PdfStamper stamper = new PdfStamper(reader, result );
		 
		over  = stamper.getOverContent(1); 
		
//		printOver(documento, rcv, over, null);

		stamper.close();  
		
		templateInputStream.close();
		
		return result;
		 
	}
	
	*/

	 
	private ByteArrayOutputStream printPrimaRata(  String var ) throws Exception{
		ByteArrayOutputStream result =  new ByteArrayOutputStream();
		PdfReader reader;  
		PdfContentByte over;
		InputStream templateInputStream;
		
		templateInputStream = getTemplateInputStreamPrimaRata();
		
		reader = new PdfReader( templateInputStream );
		
		PdfStamper stamper = new PdfStamper(reader, result );
		 
		over  = stamper.getOverContent(1); 

		printOverSingolaRata(  over   );

		stamper.close();  
		
		templateInputStream.close();
		
		return result;
		 
	}
	 

	 
	private ByteArrayOutputStream printSingolaRata(  Documento documento , RataDocumento rataDocumento) throws Exception{
		ByteArrayOutputStream result =  new ByteArrayOutputStream();
		PdfReader reader;  
		PdfContentByte over;
		InputStream templateInputStream;
		
		templateInputStream = getTemplateInputStreamPrimaRata();
		
		reader = new PdfReader( templateInputStream );
		
		PdfStamper stamper = new PdfStamper(reader, result );
		 
		over  = stamper.getOverContent(1); 
		
		printOverSingolaRata(  over   );

		stamper.close();  
		
		templateInputStream.close();
		
		return result;
		 
	}
	  


	public byte[] generateBarCode128( )
			throws IOException {

		byte[] result;
		
		ByteArrayOutputStream out = null;

		out = new ByteArrayOutputStream();
		 
		try {

			// Create the barcode bean

			int dpi = 300;

			Code128Bean bean = new Code128Bean();

			bean.setCodeset(Code128Constants.CODESET_A);
			
			bean.setBarHeight(12);  
			bean.setModuleWidth(0.3);
			bean.doQuietZone(false);
			
//			bean.setFontName("Arial");
//			bean.setFontSize(6.94); // 59 pixel / 8.5
//			bean.setHeight(32.93); // 389 pixel / 300 dpi * 25.4
			
			bean.setMsgPosition(HumanReadablePlacement.HRP_NONE); 
   
			BitmapCanvasProvider canvas = new BitmapCanvasProvider(out,
							MimeTypes.MIME_PNG, dpi, BufferedImage.TYPE_BYTE_BINARY,
							false, 0);

			// Generate the barcode 
			bean.generateBarcode(canvas, "111111111111234");
  
			canvas.finish();

		} catch (IOException e) {

			throw e;

		} finally{
			result = out.toByteArray();
		}
		
		
		return result;
	}
 
	

	static public byte[] generateDataMatrix(String message) throws IOException {

		byte[] result;
		
		ByteArrayOutputStream out = null;

		out = new ByteArrayOutputStream();
		
		try { 
 
			int dpi = 300;
			
			DataMatrixBean bean = new DataMatrixBean();
			
			bean.setShape(SymbolShapeHint.FORCE_RECTANGLE);
			   
			bean.setMinSize( new java.awt.Dimension(48 ,16 ) );
			bean.setMaxSize( new java.awt.Dimension(48 ,16 ) );
			
			bean.setModuleWidth(0.9315);
			
			bean.doQuietZone(false);
			   
			// Set up the canvas provider for monochrome JPEG output
			BitmapCanvasProvider canvas = new BitmapCanvasProvider(out,
					MimeTypes.MIME_PNG, dpi, BufferedImage.TYPE_BYTE_BINARY,
					false, 0);
 

			bean.generateBarcode(canvas, message); 

			// Signal end of generation
			canvas.finish();

		} catch (IOException e) {

			throw e;

		} finally{
			result = out.toByteArray();
		}
		
		
		return result;
	}
	
	
	public static String formatDecimalNumber(BigDecimal bdValue)
	{
		DecimalFormat dcFormat = getDecimalFormat();
		bdValue = bdValue.setScale(2, BigDecimal.ROUND_HALF_UP);
		
		return dcFormat.format(bdValue);
	}
	
	private String format6Digits(double d){  
		return _6Digits.format( d );
	}

	
	public static DecimalFormat getDecimalFormat()
	{
		DecimalFormatSymbols symbols = new DecimalFormatSymbols(); 
		symbols.setDecimalSeparator(',');
		symbols.setGroupingSeparator('.');

		DecimalFormat dcFormat = new DecimalFormat("#0.00", symbols);
		return dcFormat;
	}
	
	/*
	private static String formatMoney(Integer money){
		return "MONEY!!";
	}*/
	
	public static String formatCalendarData(Calendar data, String sFormato)
	{
		if (data != null)
		{
			Calendar cal = Calendar.getInstance(Locale.ITALIAN);
			cal.setTime(data.getTime());
		
			SimpleDateFormat formatDateTime = new SimpleDateFormat(sFormato);
			return formatDateTime.format(cal.getTime());
		}
		else
			return "";
	}
	
	private String getReplace(String cara, String den)  {
		char[] caratteri = cara.replaceAll(" ", "").toCharArray();
		for (int i = 0; i < caratteri.length; i++) {
			String b=Character.toString(caratteri[i]) ;
			den = den.replace(b, "");
		}
		return den;
	}
	
	//CG RE130450 BARCODE GS1_128.
    public byte[] generateEAN128(String message) throws IOException {
    	byte[] result;
		
		ByteArrayOutputStream out = null;

		out = new ByteArrayOutputStream();
        try {
             // Create the barcode bean
             int dpi = 300; 
             org.krysalis.barcode4j.impl.code128.EAN128Bean ean128Bean = new EAN128Bean();
          
             //ean128Bean.setHeight(29.1);
             ean128Bean.setHeight(36.1);
             //ean128Bean.setModuleWidth(0.6488);
             ean128Bean.setModuleWidth(0.6625);
             ean128Bean.setTemplate("(415)n13(8020)n18(3902)n6");
               
//             char checkDigitMarker = 0x00f0;
//             ean128Bean.setCheckDigitMarker(  checkDigitMarker );
//             char groupSeparator = 0x00f1;
//             ean128Bean.setGroupSeparator( groupSeparator );
             ean128Bean.doQuietZone(false);
             ean128Bean. setMsgPosition(HumanReadablePlacement.HRP_NONE);
//              <barcode>
//               <ean-128>
//                   <height>{length:15mm}</height>
//                   <module-width>{length:0.21mm}</module-width>
//                   <check-digit-marker>{char:&#x00f0}</check-digit-marker>
//                   <group-separator>{char:&#x00f1}</group-separator>
//                   <template>{string}</template>
//                   <quiet-zone enabled="{boolean:true}">{length:10mw}</quiet-zone>
//                   <human-readable>
//                     <placement>{human-readable-placement:bottom}</placement>
//                     <font-name>{font-name:Helvetica}</font-name>
//                     <font-size>{length:8pt}</font-size>
//                     <omit-brackets>{boolean:false}</omit-brackets>
//                   </human-readable>
//               </ean-128>
//               </barcode>
               // Open output file
               //out = new FileOutputStream(outputFile);
               // Set up the canvas provider for monochrome JPEG output
               BitmapCanvasProvider canvas = new BitmapCanvasProvider(out,
                             MimeTypes.MIME_PNG, dpi, BufferedImage.TYPE_BYTE_BINARY,
                             false, 0); 
               ean128Bean.generateBarcode(canvas, message); 

               canvas.finish();
        } catch (IOException e) {
			throw e;
		} finally{
			result = out.toByteArray();
		}
		return result;
	}
}
